# -*- coding: utf-8 -*-
from django.contrib.auth.models import AnonymousUser
from django.db import models
from django.template import RequestContext
from django.contrib.auth.models import User
from django.shortcuts import render_to_response, redirect
#Importamos modulos propios
from mundofacu.usuarios.models import amonestaciones

#Decoradores
def login_required(vista):
	def logedvista(*args,**kargs):
		if args[0].user.is_authenticated(): return vista(*args,**kargs)
		else: return redirect('/login_required')
	return logedvista


def login_unrequired(vista):
	def unlogedvista(*args,**kargs):
		if isinstance(args[0].user,AnonymousUser): return vista(*args,**kargs)
		else: return render_to_response('error.html', {'title':"Logout", 'msg': 'Debe estar loggeado para salir.'}, context_instance=RequestContext(*args,**kargs))
	return unlogedvista


def staff_required(vista):
	def adminvista(*args,**kargs):
		if args[0].user.is_staff: return vista(*args,**kargs)
		else: return redirect('/staff_required')
	return adminvista


def under_construction(vista):
	def vista_en_construccion(*args,**kargs):
		return redirect('/under_construction')
	return vista_en_construccion


def mod_required(vista):
	def adminvista(*args,**kargs):
		if args[0].user.is_staff: return vista(*args,**kargs)
		else: return redirect('/mod_required')
	return adminvista


def staff_required(vista):
	def adminvista(*args,**kargs):
		if args[0].user.is_staff: return vista(*args,**kargs)
		else: return redirect('/staff_required')
	return adminvista

def muted(vista):
	def silenciada(*args,**kargs):
		if amonestaciones.objects.filter(user=args[0].user, estado=1).count() > 0: return redirect("/muted")
		return vista(*args,**kargs)
	return silenciada